package com.jiguang.shangcheng.servlet;

import com.jiguang.shangcheng.dao.AdminDao;
import com.jiguang.shangcheng.pojo.Order;
import com.jiguang.shangcheng.pojo.Product;
import com.jiguang.shangcheng.pojo.User;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.math.BigDecimal;

@WebServlet("/shang/shang_selectOrderInfo")
public class selectOrderInfo extends HttpServlet {
    @SneakyThrows
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置字符集
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("UTF-8");

        //获取订单ID
        String id = req.getParameter("id");
        HttpSession session = req.getSession();
        User auto = (User) session.getAttribute("name");

        //通过订单ID查询订单
        AdminDao adminDao = new AdminDao();
        Order order = adminDao.selectOrderByid(Integer.parseInt(id));

        //通过买家ID查询买家信息
        User buyUser = adminDao.selectUserByid(order.getUserId());
        req.setAttribute("buyUser",buyUser);

        //通过商品ID查询商品信息
        Product product = adminDao.selectProductByid(order.getProductId());
        BigDecimal two = new BigDecimal(product.getPrice());
        req.setAttribute("product",product);

        //通过卖家ID查询卖家信息
        User soleUser = adminDao.selectUserByid(product.getUserId());
        req.setAttribute("soleUser",soleUser);
        if(auto.getId().equals(soleUser.getId())){
            req.setAttribute("isSole","1");
        }else{
            req.setAttribute("isSole","0");
        }

        req.setAttribute("order",order);

        req.getRequestDispatcher("shang_order-info.jsp").forward(req,resp);

    }
}
